import time
import streamlit as st
import function.init_page_config as fi
import function.operate_database as fdb





user_id=st.session_state.user_id
username=st.session_state.user_name
def transform(msg):
    msg.encode('utf8')
    for word in msg.split():
        yield word + " "
        time.sleep(0.5)


def chat():
    fi.init()
    backflag=st.button("返回")
    st.title("欢迎使用有问必答chatBot")
    st.subheader(f"尊敬的{username}用户，欢迎您使用Ye聊天机器人")

    result=fdb.Call_Message(user_id)
    if result:
        for msg in result:
            # Check if msg is a dictionary and has the expected keys
            #if isinstance(msg, dict) and "role" in msg and "message" in msg:
                with st.chat_message(msg["role"]):
                    st.write(msg["message"])
            #else:
              #  st.write("Unexpected message format:", msg)

    if backflag:
        time.sleep(2)
        st.switch_page("login.py")

chat()
message=st.chat_input("请输入你的问题")